function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
layui.use(['form','layer'], function () {

    var $ = layui.$, form = layui.form, layer=layui.layer;

    var page = 1;
    var limit = 20;
    var hrList;
    var userInfo;
    var hrAuditList;
    $(function () {
        LoginStatus();

        userInfo = JSON.parse($.cookie('userInfo'));

        $('#userAvatar').attr('src', companyServices + userInfo.img);
        $('#userName').text((userInfo.name == '' || userInfo.name == null) ? '':userInfo.name);
        $('#userNameOther').text((userInfo.name == '' || userInfo.name == null) ? '' : userInfo.name);
        $('#userPhone').text((userInfo.tel == '' || userInfo.tel == null) ? '无' : userInfo.tel);
        $('#userPosition').text((userInfo.position == '' || userInfo.position == null) ? '人事': userInfo.position);


        var type = getUrlParam('type');
        if (type != '' && type != null && type != 'undefined' && type == '1') {
            $('#hrManage').removeClass('layui-show');
            $('#hrExamine').addClass('layui-show');
            $('#hrManageLi').removeClass('layui-this');
            $('#hrExamineLi').addClass('layui-this');
        }

        //加载公司信息
        loadCompany();
        //加载HR信息
        loadHrList();
        //加载审核信息
        loadHrAuditList();
    })
    $('#logout').click(function () {
        layer.confirm('您确认退出登录吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            logoutCompany();
        }, function(){

        });
    })
    window.delHr = function(hrId) {
        layer.confirm('是否确认删除该hr？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            delPosition(hrId)
        }, function(){

        });
    }


    window.pass = function(id) {
        layer.confirm('确定同意该HR的申请吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            handleHr(id, 1);
        }, function(){

        });
    }

    window.noPass = function(id) {
        layer.confirm('确定驳回该HR的申请吗？', {
            btn: ['确定','取消'] //按钮
        }, function(){
            handleHr(id, 0);
        }, function(){

        });
    }

    function handleHr(id, type) {
        var url = '';
        if (type == 1) {
            url = companyServices+"/hr/audit/admit/" + id;
        } else {
            url = companyServices+"/hr/audit/reject/" + id;
        }

        layer.load(2);
        $.ajax({
            type: 'post', // 提交方式 get/post
            url:url,
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data: {}
            ,success: function(res) {
                layer.closeAll('loading');

                checkLoginStatus(res);

                if(res.status == '00200'){
                    layerMsg(res.message);
                    setTimeout(function() {
                        window.location.href="hr-manage.html?type=1" ;
                    }, 2000);

                }else{
                    layerMsg(res.message)
                }
            },fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    function delPosition(hrId) {
        layer.load(2);
        $.ajax({
            type: 'delete', // 提交方式 get/post
            url: companyServices+"/hr/" + hrId, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data: {}
            ,success: function(res) {
                layer.closeAll('loading');

                checkLoginStatus(res);

                if(res.status == '00200'){
                    layerMsg(res.message);
                    setTimeout(function() {
                        window.location.href="hr-manage.html" ;
                    }, 2000);

                }else{
                    layerMsg(res.message)
                }
            },fail:function () {
                layer.closeAll('loading');
            }
        })
    }


    $('#addPosition').click(function () {
        window.location.href="job-manage.html";
    })

    //加载公司信息
    function loadCompany() {
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/company", // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                checkLoginStatus(res);

                if(res.status == '00200'){
                    var companyInfo = res.data;
                    //$.cookie("companyInfo", JSON.stringify(companyInfo));
                    $('#companyLogo').attr('src', companyServices + companyInfo.logo);
                    $('#companyFullName').text((companyInfo.fullName == '' || companyInfo.fullName == null) ? '':companyInfo.fullName);
                    $('#companySimpleName').text((companyInfo.simpleName == '' || companyInfo.simpleName == null ) ? '':companyInfo.simpleName);
                    $('#industry').text((companyInfo.industryName == '' || companyInfo.industryName == null) ? '':companyInfo.industryName);
                    $('#companyLevel').text((companyInfo.companySizeLevelName == '' || companyInfo.companySizeLevelName == null ) ? '':companyInfo.companySizeLevelName);


                    if (companyInfo.rootId === userInfo.id) {
                        $('#hrManager').show();
                    } else {
                        window.history.go(-1);
                    }
                }
            }
        })
    }

    //加载职位列表
    function loadHrList() {
        layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/hr/list?page="+page+"&limit=" + limit, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                layer.closeAll('loading');
                checkLoginStatus(res);

                if(res.status == '00200'){
                    hrList = res.data;

                    var html = '';
                    if (hrList != null && hrList.length > 0) {
                        $.each(hrList, function (index, item) {
                            html += '<li>';
                            html += '<a href="javascript:">';
                                html += '<div class="delete-hr-btn" onclick="delHr(\''+item.userId+'\')"></div>';
                                html += '<div class="card-con1">';
                                html +=  '<div class="pic">'
                                html += '<img src="'+companyServices+item.userImg+'">';
                                html += '</div>';
                                html += '<div class="word-detail">';
                                html += '<h2>'+((item.userName == "" || item.userName == null) ? "": item.userName)+'</h2>';
                                html += '<p>'+((item.userPosition == "" || item.userPosition == null) ? "" : item.userPosition) +'</p>';
                                html += '<span class="phone">'+((item.userTel == "" || item.userTel == null) ? "":item.userTel)+'</span>';
                                html += '</div>';
                                html += '</div>';
                                html += '<p class="card-con3">';
                                html += '<span>'+dateFormat("yyyy-MM-dd",new Date(item.userCreateTime))+'</span>';
                                html += '<span class="email" title="'+((item.userEmail == "" || item.userEmail) == null ? "":item.userEmail)+'">'+((item.userEmail == "" || item.userEmail) == null ? "":item.userEmail)+'</span>';
                                html += '</p> </a> </li>';
                        })

                        $('.hr-manage').html(html);

                    }
                }
            }, fail:function () {
                layer.closeAll('loading');
            }
        })
    }

    function loadHrAuditList() {
        //layer.load(2);
        $.ajax({
            type: 'get', // 提交方式 get/post
            url: companyServices+"/hr/audit/list?page="+page+"&limit=" + limit, // 需要提交的 url
            contentType:"application/json;charset=utf-8",
            xhrFields: {
                withCredentials: true
            }
            ,crossDomain: true
            ,data:{},
            success: function(res) {
                layer.closeAll('loading');
                checkLoginStatus(res);

                if(res.status == '00200'){
                    hrAuditList = res.data;

                    var html = '';
                    if (hrAuditList != null && hrAuditList.length > 0) {
                        $.each(hrAuditList, function (index, item) {
                            html += '<li> <a href="javascript:">';
                                html +='<div class="pass-hr-btn" onclick="pass(\''+item.id+'\')"></div>';
                                html +='<div class="delete-hr-btn" onclick="noPass(\''+item.id+'\')"></div>';
                                html +='<div class="card-con1">';
                                html +='<div class="pic">';
                                html +='<img src="'+companyServices + item.hrImg +'">';
                                html +='</div> <div class="word-detail">';
                                html +='<h2>'+((item.hrName == "" || item.hrName == null) ? "" : item.hrName)+'</h2>';
                                html +='<p>'+((item.hrPosition == "" || item.hrPosition == null) ? "":item.hrPosition);
                                if (item.status == 0) {
                                    html +='<span>[未审核]</span>';
                                } else {
                                    html +='<span>[已审核]</span>';
                                }

                                html+='</p>';
                                html +='<span class="phone">'+((item.hrTel == "" || item.hrTel == null) ? "":item.hrTel)+'</span>';
                                html +='</div> </div>';
                                html +='<p class="card-con3">';
                                html +='<span>'+dateFormat("yyyy.MM.dd",new Date(item.createTime))+'</span>';
                                html +='<span class="email">'+((item.hrEmail == "" || item.hrEmail == null) ? "":item.hrEmail)+'</span>';
                                html +='</p> </a> </li>';
                        })

                        $('.hr-examine').html(html);

                    }
                }
            }, fail:function () {

            }
        })
    }

    /**************************************时间格式化处理************************************/
    function dateFormat(fmt,date) {
        var o = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours(),                   //小时
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
});




